import art from './index.art'
const {getUser} = require('../../utils/storageUtils.js')
import axios from '../../api/request.js'

async function changePage(page){
    const pageNow = page || 1
    const limit = 5
    const {data:{users,total}} = await axios({
        url:`/admin/user/${pageNow}/${limit}`
    })
    
    const pageCount = Math.floor((total/limit)+1)
    // console.log(pageCount);
    // console.log(getUser());
    
    const html = art({username:getUser().username,users,pageCount,pageNow})
    // console.log(typeof art);
    // console.log(html);
    
    document.getElementById('app').innerHTML=html
    // console.log(5);
    //删除用户
    handleClickDelete(page)
}
changePage(1)

window.changePage = changePage
window.updataPage = updataPage

function handleClickDelete(page){
    //删除用户
const delBtns = document.querySelectorAll('.delBtn')
for(let i = 0;i < delBtns.length;i++){
    delBtns[i].addEventListener('click',async function(e){
        if(!confirm('确定删除吗？')) return;
        const {id} = e.target.dataset
        // console.log(id);
        // console.log(typeof id);
        const {data} = await axios({
            url:`/admin/user/${id}`,
            method:'delete'
        })
        // console.log(data);
        if(data.code === 2000){
            alert('删除成功')
            changePage(page)
        }
    })
}
}

function updataPage(id){
    window.location.href=`userUpdate.html#${id}`
}